// JavaScript Document
/*$.ajaxSetup({
		ajaxStart: loading(),
		ajaxComplete: endloading()
})*/

function loading_start(){
	$("#load_status").fadeIn()
}

function loading_end(){
	$("#load_status").fadeOut()
}
thumbs_viewer = ''

Array.prototype.count = function() {
    return this.length;
};

function loading(){
	alert("retrieving data")
}
function endloading(){
	alert("loading complete")
}
function galleries_index(){
	$("ul#subMenu").remove()
	galleries_JSON = JSON.parse($.ajax({
		url:"scripts/get_json_gallery_list.php?id=1",
		async:false
	}).responseText)
	$("<ul>").attr({
		"id": "subMenu"
	}).appendTo($("#globalheader"))

	$("li#gn-works a").attr({
		"href": "#works@"+galleries_JSON.items[0].gallery_id
	})

	$.each(galleries_JSON.items,function(i,item){
		if(item.gallery_id == 100){
			return
		}
		$("<li><a class='menu' href='#works@"+item.gallery_id+"'>"+item.gallery_title+"</a></li>").appendTo($("#subMenu"))
	})
}

function thumbs(gallery_active){
//	var width = new Number() // con questa variabile controllo se la larghezza del contenitore e di conseguenza inserisco i pulsanti di scorrimento


	if(thumbs_viewer == ''){
		thumbs_viewer = $.ajax({
			url: "pages/thumbs_viewer.php",
			async: false
		}).responseText
	}
	$("#mainContent").html(thumbs_viewer)
	$("<div/>").attr({
		"id":"photo_viewer_container"
	}).prependTo($("#mainContent"))

	$.getJSON("scripts/get_json_gallery.php?id="+gallery_active,
		function(data){
			var width = new Number()
			var page = new Number(0)
			$("<img/>").attr({
				"src": "photos/"+data.gallery_items[0].photo_hash+".png",
				"alt": data.gallery_items[0].photo_title,
				"title": data.gallery_items[0].photo_title,
				"id": "photo_viewer"
			}).appendTo($("#photo_viewer_container"))
			$("#photo_title").html(data.gallery_items[0].photo_title)
			$("#photo_description").html(data.gallery_items[0].photo_description)
			$("#photo_date").html(data.gallery_items[0].photo_date)
			$("#photo_px").html(data.gallery_items[0].photo_width+" X "+data.gallery_items[0].photo_height)
			$.each(data.gallery_items,function(i,item){
				$("<img />").attr({
					"src": "photos/"+item.photo_hash+".thumb.png",
					"id": "photo_thumb_"+item.photo_id,
					"alt": item.photo_title,
					"title": item.photo_title
				}).css({
					"position":"relative"
				}).click(function(){
					$("#photo_title").html(item.photo_title)
					$("#photo_description").html(item.photo_description)
					$("#photo_date").html(item.photo_date)
					$("#photo_px").html(item.photo_width+" X "+item.photo_height)
					$("#mainContent div#photo_viewer_container").html("")
					$("<img/>").attr({
						"id": "photo_viewer",
						"src": "photos/"+item.photo_hash+".png",
						"alt": item.photo_title,
						"title": item.photo_title
					}).appendTo($("div#photo_viewer_container"))

				}).appendTo($("#thumbs"))
				width += $("#photo_thumb_"+item.photo_id).width()
				pages = Math.round((width/798)+0.5)
			})
			if(width > 798){
				$("#right_scroll").fadeIn().click(function(){
					page += 1
					$("#thumbs img").animate({
						"left": "-=798px"
					})
					if(pages == 2){
						if(page == 1){
							$("#right_scroll").fadeOut()
						}
					}
					if((pages -1) == page){
						$("#right_scroll").fadeOut()
					}
					$("#left_scroll").css({
						"visibility": "visible",
						"display":"none"
					}).fadeIn()
				})
				$("#left_scroll").click(function(){
					page-=1
					$("#thumbs img").animate({
						"left": "+=798px"
					})
					if(page < 1){
						$("#left_scroll").css({
						"visibility": "hidden",
						"display":"block"
					})
					}
					$("#right_scroll").fadeIn()

				})
			}
		}
	)
	/*
	Scrollers
	*/
}
function clearPhotoInfo(){
	$("#photo_title").html("")
	$("#photo_description").html("")
	$("#photo_date").html("")
	$("#photo_px").html("")
}
function viewEvents(date_to){
	//json_url = "scripts/get_json_events.php?uid=1";
	if(date_to != "null"){
		json_url += "&to="+date_to
	}
	events_JSON = JSON.parse($.ajax({
		url: json_url,
		async: false
	}).responseText)

	var res
	$.each(events_JSON.items,function(i,item){
		$("#mainContent").append($("<div class='event'><h2>"+item.event_title+"</h2><h3>"+item.event_start+" - "+item.event_end+"</h3><p>"+item.event_description+"</p><p>Sito web: <a href='"+item.event_website+"' target='_blank'>"+item.event_website+"</a></p></div>"))
	})
	$("#loading").fadeOut()
	return events_JSON
}

function userProfile(uId){
	d = new Date()
	seconds = parseInt(d.getTime()/1000)
	events_JSON = viewEvents(seconds)
	$("#mainContent").html("").append($("<h1>Bio</h1><p>"+events_JSON.bio+"</p><h2>Elenco mostre passate:</h2>"))

	$.each(events_JSON.items,function(i,item){
		$("#mainContent").append($("<div class='event'><h2>"+item.event_title+"</h2><h3>"+item.event_start+" - "+item.event_end+"</h3><p>"+item.event_description+"</p><p>Sito web: <a href='"+item.event_website+"' target='_blank'>"+item.event_website+"</a></p></div>"))
	})
}


function printEvents(json){
	/*
	$.each(json.items,function(i,item){

		$("<div />").attr({
			"class": "event",
			"id": "event_"+item.event_id
		}).appendTo($("#mainContent"))


		title = $("<h2/>").attr().html(item.event_title+" <small>("+item.event_start+" - "+item.event_end+")</small>").appendTo($("#event_"+item.event_id))

		description = $("<div/>").attr({
			"class":"event_description",
			"id": "event_description_"+item.event_id
		}).html(
			$("<p/>").html(item.event_description)
		).appendTo($("#event_"+item.event_id))

		website = $("<p/>").html("Sito web: <a href=\""+item.event_website+"\" target=\"_blank\">"+item.event_website+"</a>").appendTo(description)

	//	$("#mainContent").append(container)
	})
	*/
}

function getJsonParsed(jUrl){
	/*
	 * Restituisce un Json parsed a partire da un url
	 */
	json = JSON.parse($.ajax({
	 	url: jUrl,
	 	async: false
	 }).responseText)
	 return json
}

function getPreviousEvents(){
	/*
	 * Prende gli eventi passati
	 */
	 d = new Date()
	 seconds = parseInt(d.getTime()/1000)
	 eUrl = "scripts/get_json_events.php?uid=1&to="+seconds
	 json = getJsonParsed(eUrl)
	 return json
}

function getNextEvents(){
	/*
	 * Prende gli eventi futuri
	 */
	 d = new Date()
	 seconds = parseInt(d.getTime()/1000)
	 eUrl = "scripts/get_json_events.php?uid=1&from="+(seconds)
	 json = getJsonParsed(eUrl)
	 printEvents(json)
	 return json
}

function getGapEvents(gIn,gOut){
	/*
	 * Prende gli eventi compresi tra gIn, gOut
	 */
	 d = new Date()
	 eUrl = "scripts/get_json_events.php?uid=1"
//	eUrl = "tmp/get_json_events.json"
	 if(gIn){
	 	eUrl += "&from="+gIn
	 }
	 if(gOut){
	 	eUrl += "&to="+gOut
	 }
	 json = getJsonParsed(eUrl)
	 printEvents(json)
	 return json
}

function biography(){
	json = getPreviousEvents()
	$("#mainContent").html("<h1>Biography</h1>"+json.bio)
	return json
}